package com.example.administrator.yanfoxconn.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.bean.LMIE8S;
import com.example.administrator.yanfoxconn.bean.UserMessage;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.FileUtil;
import com.example.administrator.yanfoxconn.utils.HttpConnectionUtil;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 安全部 異常處置區
 * 工業安全 鋰電池防火共用
 * IE8S稽核
 * Created by wang on 2021/01/20.
 */

public class GCMainActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.btn_scan)
    Button btnViolation;//日常消殺點檢  鋰電池充電區 交叉稽核
    @BindView(R.id.btn_write)
    Button btnTurnover;//體症異常追蹤 鋰電池儲存區   季度稽核
    @BindView(R.id.btn_ocr)
    Button btnVehicle;//處置後消殺點檢  鋰電池使用區 專項稽核
    @BindView(R.id.btn_self)
    Button btnSelf;//處置後消殺點檢  充電樁點檢
    @BindView(R.id.btn_search)
    Button btnSearch;//已有記錄查詢 上報無法點檢異常

    String type="";//類別

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cross_mian);
        ButterKnife.bind(this);

        type=getIntent().getStringExtra("type");
        if(type!=null&&type.equals("FX")){
            btnSelf.setVisibility(View.VISIBLE);
            btnSearch.setVisibility(View.VISIBLE);
            tvTitle.setText("鋰電池防火");
            btnViolation.setText("充電區點檢");
            btnTurnover.setText("儲存區點檢");
            btnVehicle.setText("使用區點檢");
            btnSelf.setText("充電樁點檢");
            btnSearch.setText("無法點檢");
        }else if (type.equals("LM")){//IE8S 巡更管理
            tvTitle.setText("8S巡更管理");
            btnViolation.setText("交叉稽核");
            btnTurnover.setText("季度稽核");
            btnVehicle.setText("專項稽核");
            getRole(FoxContext.getInstance().getLoginId());
        }else if (type.equals("LW")){//總務-隱患通報
            tvTitle.setText("隱患通報");
            btnViolation.setText("自查自糾");
            btnTurnover.setText("專案稽核");
            btnVehicle.setText("異常處理");
        }else{
            tvTitle.setText("異常處置區");
            btnViolation.setText("日常消殺點檢");
            btnTurnover.setText("體症異常追蹤");
            btnVehicle.setText("處置後消殺點檢");
        }

        btnViolation.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnViolation.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnTurnover.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnTurnover.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnVehicle.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnVehicle.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnSearch.setBackgroundColor(getResources().getColor(R.color.color_f5a306));
        btnSearch.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnSelf.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnSelf.setTextColor(getResources().getColor(R.color.color_eeeeee));

        btnBack.setOnClickListener(this);
        btnViolation.setOnClickListener(this);
        btnTurnover.setOnClickListener(this);
        btnVehicle.setOnClickListener(this);
        btnSelf.setOnClickListener(this);
        btnSearch.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        if(type!=null&&type.equals("FX")){
            switch (view.getId()){
                case R.id.btn_scan://充電區點檢
                    FoxContext.getInstance().setType("FX");
                    Intent intent = new Intent(GCMainActivity.this,QrCodeActivity.class);
                    intent.putExtra("title", "掃描二維碼");
                    intent.putExtra("num", "cz");
                    startActivity(intent);
                    break;
                case R.id.btn_write://儲存區點檢
                    FoxContext.getInstance().setType("HC");
                    Intent intent2 = new Intent(GCMainActivity.this,QrCodeActivity.class);
                    intent2.putExtra("title", "掃描二維碼");
                    intent2.putExtra("num", "cz");
                    startActivity(intent2);

                    break;
                case R.id.btn_ocr://使用區點檢
                    FoxContext.getInstance().setType("HD");
                    Intent intent3 = new Intent(GCMainActivity.this,QrCodeActivity.class);
                    intent3.putExtra("title", "掃描二維碼");
                    intent3.putExtra("num", "cz");
                    startActivity(intent3);
                    break;
                case R.id.btn_self://無法點檢
                    FoxContext.getInstance().setType("HK");
                    Intent intent5 = new Intent(GCMainActivity.this,QrCodeActivity.class);
                    intent5.putExtra("title", "掃描二維碼");
                    intent5.putExtra("num", "cz");
                    startActivity(intent5);
                    break;
                case R.id.btn_search://充電樁點檢
                    Intent intent4 = new Intent(GCMainActivity.this,AbnormalCantCheckActivity.class);
                    startActivity(intent4);
                    break;
                case R.id.btn_title_left:
                    finish();
                    break;

            }
        }else if (type.equals("LM")) {//IE8S 巡更管理
            switch (view.getId()){
                case R.id.btn_scan://交叉稽核
                    FoxContext.getInstance().setType("JX");
                    Intent intent = new Intent(GCMainActivity.this,LMListActivity.class);
                    intent.putExtra("title", "交叉稽核");
                    intent.putExtra("flag1", "jcjh");
                    startActivity(intent);
                    break;
                case R.id.btn_write://季度稽核
                    FoxContext.getInstance().setType("JD");
                    Intent intent2 = new Intent(GCMainActivity.this,LMMenuListActivity.class);
                    intent2.putExtra("title", "季度稽核");
                    intent2.putExtra("flag1","jdjh");
                    startActivity(intent2);
                    break;
                case R.id.btn_ocr://專項稽核
                    FoxContext.getInstance().setType("ZX");
                    Intent intent3 = new Intent(GCMainActivity.this,LMMenuListActivity.class);
                    intent3.putExtra("title", "專項稽核");
                    intent3.putExtra("flag1", "zxjh");
                    startActivity(intent3);
                    break;
                case R.id.btn_title_left:
                    finish();
                    break;
            }
        }else if (type.equals("LW")) {//總務-隱患通報
            switch (view.getId()){
                case R.id.btn_scan://自查自糾
                    Intent intent = new Intent(GCMainActivity.this,LWUpActivity.class);
                    intent.putExtra("title", "自查自糾");
                    intent.putExtra("flag1", "zxzj");
                    startActivity(intent);
                    break;
//                case R.id.btn_write://專案稽核
//                    FoxContext.getInstance().setType("JD");
//                    Intent intent2 = new Intent(GCMainActivity.this,LWListActivity.class);
//                    intent2.putExtra("title", "專案稽核");
//                    intent2.putExtra("flag1","jdjh");
//                    startActivity(intent2);
//                    break;
//                case R.id.btn_ocr://異常處理
//                    FoxContext.getInstance().setType("ZX");
//                    Intent intent3 = new Intent(GCMainActivity.this,LWDetailActivity.class);
//                    intent3.putExtra("title", "異常處理");
//                    intent3.putExtra("flag1", "zxjh");
//                    startActivity(intent3);
//                    break;
                case R.id.btn_title_left:
                    finish();
                    break;
            }
        }else{
            switch (view.getId()){
                case R.id.btn_scan://日常消殺點檢
                    FoxContext.getInstance().setType("HA");
                    Intent intent = new Intent(GCMainActivity.this,QrCodeActivity.class);
                    intent.putExtra("title", "掃描二維碼");
                    intent.putExtra("num", "cz");
                    startActivity(intent);
                    break;
                case R.id.btn_write://體症異常追蹤
                    Intent intent2 = new Intent(GCMainActivity.this,CrossScanMainActivity.class);
                    intent2.putExtra("flag","health");
                    startActivity(intent2);
                    break;
                case R.id.btn_ocr://處置後消殺點檢
                    FoxContext.getInstance().setType("HB");
                    Intent intent3 = new Intent(GCMainActivity.this,QrCodeActivity.class);
                    intent3.putExtra("title", "掃描二維碼");
                    intent3.putExtra("num", "cz");
                    startActivity(intent3);
                    break;
                case R.id.btn_title_left:
                    finish();
                    break;

            }
        }

    }

    /*IE8S 獲取登錄者權限*/
    public void getRole(String id) {

        showDialog();
        final String url = Constants.HTTP_LM_LOGIN  ;

        JsonObject object = new JsonObject();
        object.addProperty("username", id);

        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    Gson gson = new Gson();
                    Log.e("---------", "result==fff===" + result);
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("code").getAsString();
                        if (errCode.equals("200")) {
                            JsonObject jsonObject1  = jsonObject.get("data").getAsJsonObject();
                            JsonObject jsonObject2 = jsonObject1.get("login1").getAsJsonObject();

                            Log.e("---------", "jsonObject2==fff===" + jsonObject2);
                            FoxContext.getInstance().setDepart(jsonObject2.get("depart").getAsString());
                            FoxContext.getInstance().setFlag(jsonObject2.get("flag").getAsString());
                            FoxContext.getInstance().setPermission_type(jsonObject2.get("permission_type").getAsString());
                            FoxContext.getInstance().setProduce_place(jsonObject2.get("produce_place").getAsString());

                        } else{
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        }
                    }else{
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case Constants.MESSAGE_TOAST://Toast
                    ToastUtils.showShort(GCMainActivity.this, msg.obj.toString());
                    break;
                case Constants.MESSAGE_NETMISTAKE://Toast
                    ToastUtils.showLong(GCMainActivity.this,R.string.net_mistake);
                    break;
            }
            super.handleMessage(msg);
        }
    };


}
